package com.he.controller;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.he.pojo.Signin;
import com.he.pojo.SigninExample;
import com.he.pojo.User;
import com.he.pojo.UserExample;
import com.he.service.signin.SigninServiceI;
import com.he.service.user.UserServiceI;

@Controller
@RequestMapping("signin")
public class signinController {

	@Autowired
	SigninServiceI sginService;
	@Autowired
	UserServiceI userServiceI;
	
	public String getIp() throws Exception {
		InetAddress ia=null;
            ia=ia.getLocalHost();
            String localname=ia.getHostName();
            String localip=ia.getHostAddress();
            return localip;
	}
	@RequestMapping("showMe")
	public String show(HttpServletRequest request, Signin sign,HttpSession session){
		User user=(User) session.getAttribute("user");
		int userId=user.getId();
		SigninExample signinExample=new SigninExample();
		signinExample.createCriteria().andUserIdEqualTo(userId);
		signinExample.setOrderByClause("signin_date desc");
		List<Signin> list =sginService.getPageSignins(signinExample, sign);
		
		request.setAttribute("list", list);
		/*为log总页数赋值,向前端传总页数*/
		PageInfo<Signin> info=new PageInfo<>(list);
		//为log总页数赋值
		sign.setPages(info.getPages());
		//为log总记录数赋值
		sign.setTotal(info.getTotal());
		/*为log返回的controller链接赋值*/
		sign.setUrl("showMe.do");
		/*向前端传参数保存在log中*/
		request.setAttribute("sign", sign);
		return "myAttendance";
	}
	@RequestMapping("showAll")
	public String showAll(HttpServletRequest request, Signin sign,HttpSession session){
		User S_user=(User) session.getAttribute("user");
		int departmentId=S_user.getDepartmentId();
		User user=new User();
		UserExample userExample=new UserExample();
		userExample.createCriteria().andDepartmentIdEqualTo(departmentId);
		List<User> uList=userServiceI.getUsers(userExample, user);
		List<Signin> signins=new ArrayList<>();
		for (User user2 : uList) {
			SigninExample signinExample=new SigninExample();
			signinExample.createCriteria().andUserIdEqualTo(user2.getId());
			signinExample.setOrderByClause("signin_date desc");
			List<Signin> list =sginService.getPageSignins(signinExample, sign);
			for (Signin signin2 : list) {
				signins.add(signin2);
			}
		}
		
		
		request.setAttribute("list", signins);
		/*为log总页数赋值,向前端传总页数*/
		PageInfo<Signin> info=new PageInfo<>(signins);
		//为log总页数赋值
		sign.setPages(info.getPages());
		//为log总记录数赋值
		sign.setTotal(info.getTotal());
		/*为log返回的controller链接赋值*/
		sign.setUrl("showAll.do");
		/*向前端传参数保存在log中*/
		request.setAttribute("sign", sign);
		return "attendance";
	}
	@RequestMapping("signin")
	@ResponseBody
	public String dosignin( HttpSession session) throws Exception{
		User user=(User) session.getAttribute("user");
		/*User user=new User();
		user.setId(3);
		user.setUserName("焦光源");*/
		Date date=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
    	String today = sdf.format(date);
    	
		SigninExample signinExample=new SigninExample();
		signinExample.createCriteria().andUserIdEqualTo(user.getId());
		List<Signin> sgList= sginService.getNoPageSignins(signinExample);
		
		for (Signin signin : sgList) {
			if (signin.getDate().equals(today)) {//判断今天是否进行过上班签到
				return "exist";
			}
		}
			Signin signin=new Signin();
			
			signin.setSigninDate(date);
			signin.setIsUp((byte) 0);
			signin.setUserId(user.getId());
			signin.setUserName(user.getUserName());
			signin.setSigninUp(date);
			signin.setIpUp(getIp());
			int r=sginService.insert(signin);
			if(r>0) {
				return "success";
			}else {
				return "defeat";
			}
		
	}
	@RequestMapping("signout")
	@ResponseBody
	public String dosignout( HttpSession session) throws Exception{
		User user=(User) session.getAttribute("user");
		/*User user=new User();
		user.setId(3);
		user.setUserName("焦光源");*/
		Date date=new Date();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//变为年月日格式
    	String today = sdf.format(date);
    	
		SigninExample signinExample=new SigninExample();
		signinExample.createCriteria().andUserIdEqualTo(user.getId());
		List<Signin> sgList= sginService.getNoPageSignins(signinExample);
		for (Signin signin : sgList) {
			if (signin.getDate().equals(today) && signin.getSigninDown()!=null) {//判断今天是否进行过下班签到
				System.out.println("--------一样的的！！！！------");
				return "exist";
			}else if(signin.getDate().equals(today)) {
				signin.setSigninDown(date);
				signin.setIpDown(getIp());
				int r=sginService.update(signin);
				if(r>0) {
					return "success";
				}else {
					return "defeat";
				}
			}
		}
			return "none";
	}
}
